package com.common.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;

public abstract class BaseWebMessage {

	
	private List<String> messages=new ArrayList<String>(0);
	
	private List<String> errors=new ArrayList<String>(0);
	
	
	public BaseWebMessage addMSGStr(String msg) {
		messages.add(msg);
		return this;
	}
	
	public BaseWebMessage addErrorMessage(String error){
		errors.add(error);
		return this;
	}
	
	public String showPage(ModelMap model) {
		toModel(model);
		return getPage();
	}
	
	public BaseWebMessage toModel(Map<String, Object> model) {
		Assert.notNull(model);
		model.put(getMSGAttrName(), messages);
		model.put(getErrorAttrName(), errors);
		return this;
	}
	

	public boolean hasMessage() {
		return  messages.size() > 0;
	}
	
	public boolean hasError(){
		return errors.size()>0;
	}

	/**
	 * 获得消息页面的地址
	 * 
	 * @return
	 */
	public abstract String getPage();
	/**
	 * 设置消息属性
	 * @return
	 */
	protected abstract String getErrorAttrName();
	
	protected abstract String getMSGAttrName();
}
